package flc.ast.activity;

import static android.view.View.GONE;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.RecordAudioAdapter;
import flc.ast.adapter.RecordPictureAdapter;
import flc.ast.adapter.RecordVideoAdapter;
import flc.ast.databinding.ActivityProRecordBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;

public class ProRecordActivity extends BaseAc<ActivityProRecordBinding> {

    private int type;
    private List<String> listShow = new ArrayList<>();
    private RecordVideoAdapter videoAdapter;
    private RecordPictureAdapter pictureAdapter;
    private RecordAudioAdapter audioAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_pro_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivProRecordBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvVideo.setOnClickListener(this);
        mDataBinding.tvPicture.setOnClickListener(this);
        mDataBinding.tvAudio.setOnClickListener(this);
        mDataBinding.tvClear.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new RecordVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);

        mDataBinding.rvPictureList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new RecordPictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);

        mDataBinding.rvAudioList.setLayoutManager(new LinearLayoutManager(mContext));
        audioAdapter = new RecordAudioAdapter();
        mDataBinding.rvAudioList.setAdapter(audioAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvVideo) {//视频
            type = 0;
            clearSelection();
            mDataBinding.tvVideo.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvVideo.setBackgroundResource(R.drawable.record_type_sel);
            getVideoData();
        } else if (id == R.id.tvPicture) {//图片
            type = 1;
            clearSelection();
            mDataBinding.tvPicture.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvPicture.setBackgroundResource(R.drawable.record_type_sel);
            getPictureData();
        } else if (id == R.id.tvAudio) {//音频
            type = 2;
            clearSelection();
            mDataBinding.tvAudio.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvAudio.setBackgroundResource(R.drawable.record_type_sel);
            getAudioData();
        } else if (id == R.id.tvClear) {//全部清空
            if (listShow.size() == 0) return;
            List<String> list = new ArrayList<>();
            if (type == 0) {
                AppSpUtils.saveVideoList(list);
                getVideoData();
            } else if (type == 1) {
                AppSpUtils.savePicList(list);
                getPictureData();
            } else if (type == 2) {
                AppSpUtils.saveAudioList(list);
                getAudioData();
            }
            ToastUtils.showShort(getString(R.string.clear_suc));
        }

    }

    @Override
    protected void initData() {
        getVideoData();
    }

    private void getVideoData() {
        listShow.clear();
        List<String> listSp = AppSpUtils.getVideoList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            videoAdapter.setList(listSp);
            mDataBinding.rvVideoList.setAdapter(videoAdapter);
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getPictureData() {
        listShow.clear();
        List<String> listSp = AppSpUtils.getPicList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            pictureAdapter.setList(listSp);
            mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(GONE);
        } else {
            mDataBinding.rvPictureList.setVisibility(GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getAudioData() {
        listShow.clear();
        List<String> listSp = AppSpUtils.getAudioList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            audioAdapter.setList(listSp);
            mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(GONE);
        } else {
            mDataBinding.rvAudioList.setVisibility(GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void clearSelection() {
        mDataBinding.tvVideo.setTextColor(Color.parseColor("#858788"));
        mDataBinding.tvVideo.setBackground(null);
        mDataBinding.tvPicture.setTextColor(Color.parseColor("#858788"));
        mDataBinding.tvPicture.setBackground(null);
        mDataBinding.tvAudio.setTextColor(Color.parseColor("#858788"));
        mDataBinding.tvAudio.setBackground(null);
        mDataBinding.rvVideoList.setVisibility(GONE);
        mDataBinding.rvPictureList.setVisibility(GONE);
        mDataBinding.rvAudioList.setVisibility(GONE);
    }

}